Back to Editorials CodeCraft β
  • Github
  • Instagram
< >

Prime Usernames

TIME LIMIT = 1 SEC.

  • CodeChef decided to gift 'Prime Usernames' to the top performers of their March Long Contest 2020. But CodeChef's administrators have tons of other work to do, so they have asked you to find out if a given username is a Prime Username or not.
    A Prime Username is a username whose number of unique characters is a prime number.
Input Output
The first line of the input contains N - the length of the username.
The second line contains the username itself. Note that the username is case-sensitive (i.e. 'a' and 'A' are considered to be different characters) and contains numbers and special characters.
Print "YES" if the given username is a Prime Username. Otherwise, print "NO" (without the double quotes).
Constraints
  • 1 ≤ N ≤ 106
Example Test Case
Input             Output
6
abcda*
YES
Solution

#include <iostream> #include<set> #include<string> using namespace std; bool isPrime(long long int n) //this function checks if a given number is prime or not. { if (n <= 1) return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false; for (long long int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } int main() { int n,i; cin>>n; string ip; cin>>ip; set<long int> unique; //in a set, only unique elements can be inserted. so after inserting // all the character fo the string, only the unique characters are left. for(i=0;i<n;i++) unique.insert(ip[i]); //inserting all the characters in the string if(isPrime(unique.size())) //size fo set is equal to number of unique characters.If it sprime, print YES cout<<"YES"; else cout<<"NO"; // else print NO return 0; }
  • #1 Thieves
  • #2 The Queue of Doubts
  • #3 Summation of Primes
  • #4 Spacious Maximus
  • #5 Samosas
  • #6 Reasonably Sound
  • #7 Product of Digits
  • #8 Prime Usernames
  • #9 Path Shifter
  • #10 Keypad Count
  • #11 Even Vowels
  • #12 Encryption
  • #13 Decryption
  • #14 Canteen Accountant
  • #15 Attendance Register
  • #16 Amazing Year

Get in touch

  • executives@codingstudio.club
  • +91 9010342360
  • Vignana Bharthi Instute of Technology
    Aushapur, Ghatkesar, Hyderabad, Telengana - India

© coding.Studio();